package com.hejia.bulbul.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hejia.alauda.utils.DateTimeUtil;
import com.hejia.alauda.utils.Pager;
import com.hejia.alauda.utils.PagerHelper;
import com.hejia.bulbul.entity.ChannelStatistic;
import com.hejia.bulbul.service.ChannelStatisticService;

/**
 * 渠道统计数据控制器
 * 
 * @author hyr
 *
 */
@Controller
@RequestMapping("channelstatistic")
public class ChannelStatisticController {

	@Resource
	private ChannelStatisticService channelStatisticService;
	
	@RequestMapping("channels")
	public String channels(
			String name,
			SearchCategory searchCategory, 
			Date startDate, 
			Date endDate, 
			Pager<ChannelStatistic> pager,
			ModelMap model){
		
		Date now = Calendar.getInstance().getTime();
		if (startDate == null) {
			startDate = DateTimeUtil.getDateDay(now);
		}else{
			startDate = DateTimeUtil.getDateDay(startDate);
		}
		if (endDate == null) {
			endDate = DateTimeUtil.getDateAndMaxTime(now);
		}else{
			endDate = DateTimeUtil.getDateAndMaxTime(endDate);
		}
		
		Pager<ChannelStatistic> channelStatistics = channelStatisticService.statisticChannels(
				name,
				startDate,
				endDate,
				pager);
		
		Calendar yesterday = Calendar.getInstance();
		Calendar day7 = Calendar.getInstance();
		Calendar day30 = Calendar.getInstance();

		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		yesterday.add(Calendar.DAY_OF_MONTH, -1);
		day7.add(Calendar.DAY_OF_MONTH, -6);
		day30.add(Calendar.DAY_OF_MONTH, -29);

		PagerHelper<ChannelStatistic> helper = new PagerHelper<>();
        helper.setPager(pager);
        Map<String, String> map = new HashMap<>();
        DateFormat secondFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("startDate", secondFormat.format(startDate));
        map.put("endDate", secondFormat.format(endDate));
        if(searchCategory != null){
        	map.put("searchCategory", searchCategory.toString());
        }
        helper.setParams(map);
        helper.setBaseUrl("/channelstatistic/channels");
        
        model.put("pagerHelper", helper);
		model.put("searchCategory", searchCategory);
		model.put("today", format.format(now));
		model.put("yesterday", format.format(yesterday.getTime()));
		model.put("day7", format.format(day7.getTime()));
		model.put("day30", format.format(day30.getTime()));
		model.put("name", name);
		model.put("startDate", startDate);
		model.put("endDate", endDate);
		model.put("channelStatistics", channelStatistics);
		
		return "channelstatistic/channels";
	}
	
	@RequestMapping("register")
	public String register(
			String name,
			SearchCategory searchCategory, 
			Date startDate, 
			Date endDate, 
			Pager<ChannelStatistic> pager,
			ModelMap model){
		
		Date now = Calendar.getInstance().getTime();
		if (startDate == null) {
			startDate = DateTimeUtil.getDateDay(now);
		}else{
			startDate = DateTimeUtil.getDateDay(startDate);
		}
		if (endDate == null) {
			endDate = DateTimeUtil.getDateAndMaxTime(now);
		}else{
			endDate = DateTimeUtil.getDateAndMaxTime(endDate);
		}
		
		Pager<ChannelStatistic> channelStatistics = channelStatisticService.statisticRegister(
				name,
				startDate,
				endDate,
				pager);
		
		Calendar yesterday = Calendar.getInstance();
		Calendar day7 = Calendar.getInstance();
		Calendar day30 = Calendar.getInstance();

		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		yesterday.add(Calendar.DAY_OF_MONTH, -1);
		day7.add(Calendar.DAY_OF_MONTH, -6);
		day30.add(Calendar.DAY_OF_MONTH, -29);

		PagerHelper<ChannelStatistic> helper = new PagerHelper<>();
        helper.setPager(pager);
        Map<String, String> map = new HashMap<>();
        DateFormat secondFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("startDate", secondFormat.format(startDate));
        map.put("endDate", secondFormat.format(endDate));
        if(searchCategory != null){
        	map.put("searchCategory", searchCategory.toString());
        }
        helper.setParams(map);
        helper.setBaseUrl("/channelstatistic/register");
        
        model.put("pagerHelper", helper);
		model.put("searchCategory", searchCategory);
		model.put("today", format.format(now));
		model.put("yesterday", format.format(yesterday.getTime()));
		model.put("day7", format.format(day7.getTime()));
		model.put("day30", format.format(day30.getTime()));
		model.put("name", name);
		model.put("startDate", startDate);
		model.put("endDate", endDate);
		model.put("channelStatistics", channelStatistics);
		
		return "channelstatistic/register";
	}

	/**
	 * 查询类别
	 * 
	 * @author hyr
	 *
	 */
	public enum SearchCategory {

		/**
		 * 今天
		 */
		TODAY,

		/**
		 * 昨天
		 */
		YESTODAY,

		/**
		 * 最近7天
		 */
		LATEST7DAY,

		/**
		 * 最近30天
		 */
		LATEST30DAY,
		
		/**
		 * 日期
		 */
		DATE,
		
		/**
		 * 全部
		 */
		ALL
	}
	
	@InitBinder
    //此处的参数也可以是ServletRequestDataBinder类型
    public void initBinder(WebDataBinder binder) throws Exception {
        //注册自定义的属性编辑器
        //1、日期
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        CustomDateEditor dateEditor = new CustomDateEditor(df, true);
        //表示如果命令对象有Date类型的属性，将使用该属性编辑器进行类型转换
        binder.registerCustomEditor(Date.class, dateEditor);

    }
}
